/*******************************************************************************
*  file    : utils.cpp
*  created : 24.06.2014
*  author  : Slyshyk Oleksiy (alexSlyshyk@gmail.com)
*******************************************************************************/

#include "utils.hpp"

bool plugin_utils::save_simple_to_file(const QString &file_name, const std::vector<std::complex<double> >& data, double time)
{
    QFile file(file_name);
    if(!file.open(QIODevice::Text | QIODevice::WriteOnly | QIODevice::Truncate))
        {
            qWarning() << "Can't open "<< file_name;
            return false;
        }

    QTextStream out(&file);
    out << data.size() << "  " << time << endl;
    for(const auto& v : data)
        out << " " << v.real() << " ";

    file.close();

    return true;
}



QStringList plugin_utils::fileList(const QString &str)
{
    QDir dir("./data");
    return dir.entryList(QStringList() << str , QDir::Files );
}


void plugin_utils::normalize(std::vector<std::complex<double> > &data, double n)
{
    if(data.size() == 0)
        {
            qDebug() << Q_FUNC_INFO << "Retreive Zero array.";
            return;
        }
    if( n > 0)
        {
            auto max = std::max_element(data.cbegin(),data.cend(),
                                        [](const std::complex<double>& d1,const std::complex<double>& d2)
            {return d1.real()<d2.real();});
            double norm_koeff;
            norm_koeff = std::real(*max) / n;

            auto begin = data.begin();
            auto end   = data.end();
            std::transform(begin, end, begin, [norm_koeff](const std::complex<double>& d){return d / norm_koeff;} );
        }
}
